package com.fanfanwang.ffw.app.adapter;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import com.android.volley.toolbox.NetworkImageView;
import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppContext;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.R.color;
import com.fanfanwang.ffw.app.bean.NewsListBean;
import com.fanfanwang.ffw.app.bean.OrdersListBean;
import com.fanfanwang.ffw.app.bean.SectionBean;
import com.fanfanwang.ffw.app.bean.ServiceBean;
import com.fanfanwang.ffw.app.bean.ServiceCarWashBean;
import com.fanfanwang.ffw.app.bean.ServiceNewBean;
import com.fanfanwang.ffw.app.ui.service.ServiceNewListDetailAct;

import android.content.Context;
import android.content.Intent;
import android.support.v4.util.TimeUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ServiceNewListAdapter extends BaseAdapter {

	private ArrayList<NewsListBean> beans=new ArrayList<NewsListBean>();
	private Context mContext;
	public ServiceNewListAdapter(Context mContext,ArrayList<NewsListBean> beans) {
		// TODO Auto-generated constructor stub
		this.mContext = mContext;
		this.beans.addAll(beans);
	}
	public void resetData(ArrayList<NewsListBean> beans){
		this.beans=beans;
	}
	public void addList(ArrayList<NewsListBean> beans){
		this.beans.addAll(beans);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beans.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_for_service_carnewlist, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.new_title);
			holder.content = (TextView) convertView.findViewById(R.id.new_content);
			holder.date = (TextView) convertView.findViewById(R.id.new_date);
			holder.imageview=(NetworkImageView)convertView.findViewById(R.id.new_image);
			holder.read_more=(TextView)convertView.findViewById(R.id.new_read_more);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(!TextUtils.isEmpty(beans.get(position).title))
		{
			holder.title.setText(AppConstant.URLDecoderString(beans.get(position).title));
		}
		if(!TextUtils.isEmpty(beans.get(position).imgurl))
		{
			holder.imageview.setDefaultImageResId(R.drawable.ic_launcher);
			holder.imageview.setErrorImageResId(R.drawable.ic_launcher);
//			holder.imageview.setImageUrl(beans.get(position).imgurl, AppContext.imageLoader);
			holder.imageview.setImageUrl(AppConstant.getHttpUrl(AppConstant.URLDecoderString(AppConstant.URLDecoderString(beans.get(position).imgurl))),  AppContext.imageLoader);
		}
		if(!TextUtils.isEmpty(beans.get(position).addtime))
		{
			holder.date.setText(com.fanfanwang.ffw.app.tools.TimeUtils.parseLongToDate(beans.get(position).addtime, "yyyy-MM-dd HH:mm"));
		}
		if(!TextUtils.isEmpty(beans.get(position).description))
		{
			holder.content.setText(AppConstant.URLDecoderString(beans.get(position).description));
		}
		holder.read_more.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//TODO　这里进行调转，或者可以回调到act，在activity里面进行跳转。
				Intent intent=new Intent(mContext,ServiceNewListDetailAct.class);
				intent.putExtra("id",beans.get(position).id);
//				intent.putExtra("data", beans.get(position));
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {

		TextView title;
		TextView content;
		TextView date;
		NetworkImageView imageview;
		TextView read_more;
	}


}
